package cn.com.aiton.educenter.controller;


import cn.com.aiton.educenter.config.OperLog;
import cn.com.aiton.educenter.entity.PestsControl;
import cn.com.aiton.educenter.entity.UcenterMember;
import cn.com.aiton.educenter.entity.vo.PestsProjectQuery;
import cn.com.aiton.educenter.entity.PestsProject;
import cn.com.aiton.educenter.service.PestsControlService;
import cn.com.aiton.educenter.service.PestsProjectService;
import cn.com.aiton.educenter.service.PestsTrapService;
import cn.com.aiton.educenter.service.UcenterMemberService;
import cn.com.qiter.common.Result;
import cn.com.qiter.common.vo.ProjectModel;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 防治项目 前端控制器
 * </p>
 *
 * @author testAuthor
 * @since 2021-01-17
 */
@RestController
@RequestMapping("/educenter/project")
public class PestsProjectController {
    //把service注入
    @Autowired
    private PestsProjectService service;

    @Autowired
    UcenterMemberService memberService;
    @Autowired
    private PestsControlService pestsControlService;

    @Autowired
    private PestsTrapService pestsTrapService;
    //1 查询讲师表所有数据
    //rest风格
    @ApiOperation(value = "所有项目列表")
//    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "所有项目列表")
    @GetMapping("findAll")
    public Result findAll() {
        //调用service的方法实现查询所有的操作
        List<PestsProject> list = service.list(null);
        List<ProjectModel> pmList = new ArrayList<>();
        for (PestsProject pp :
                list) {
            ProjectModel pm = new ProjectModel();
            BeanUtil.copyProperties(pp,pm);
            pmList.add(pm);
        }
        return Result.ok().data("items",pmList);
    }

    @ApiOperation(value = "统计")
    @OperLog(type = "统计",module = "PestsProjectController",requestDes = "统计项目的方法")
    @GetMapping("count/{projectId}")
    public Result count(@PathVariable String projectId){
        PestsProject projects = service.getById(projectId);
        Integer count = pestsControlService.countPestsByProjectId(projects.getId());
        Integer scount = pestsTrapService.countTrapByProjectId(projects.getId());
        projects.setRecordCount(count);
        projects.setEntities(scount);
        projects.setFileCount((count+scount) * 2);
        projects.setDeskCount(projects.getFileCount() * 1200);
        service.updateById(projects);

        return Result.ok().data("project",projects);
    }

    //2 逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除防治")
    @OperLog(type = "删除",module = "PestsProjectController",requestDes = "逻辑删除防治")
    @DeleteMapping("remove/{id}")
    public Result remove(@ApiParam(name = "id", value = "ID", required = true)
                    @PathVariable String id) {
        boolean flag = service.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //3 分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @ApiOperation(value = "分页查询项目列表")
    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "分页查询项目列表")
    @GetMapping("page/{current}/{limit}")
    public Result page(@PathVariable long current,
                  @PathVariable long limit) {
        //创建page对象
        Page<PestsProject> page = new Page<>(current,limit);
        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        service.page(page,null);

        long total = page.getTotal();//总记录数
        List<PestsProject> records = page.getRecords(); //数据list集合


        return Result.ok().data("total",total).data("rows",records);
    }

    //4 条件查询带分页的方法
    @ApiOperation(value = "分页查询项目列表")
    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "分页查询项目列表")
    @PostMapping("page/condition/{current}/{limit}")
    public Result pageCondition(@PathVariable long current,@PathVariable long limit,
                           @RequestBody(required = false) PestsProjectQuery query) {
        //创建page对象
        Page<PestsProject> page = new Page<>(current,limit);

        //构建条件
        QueryWrapper<PestsProject> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = query.getName();
        String begin = query.getBegin();
        String end = query.getEnd();
        String userId = query.getUserId();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }
        if(!StringUtils.isEmpty(userId)) {
            wrapper.eq("user_id",userId);
        }
        //排序
        wrapper.orderByDesc("id");

        //调用方法实现条件查询分页
        service.page(page,wrapper);

        long total = page.getTotal();//总记录数
        List<PestsProject> records = page.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);
    }

    //添加讲师接口的方法
    @ApiOperation(value = "新增项目")
    @OperLog(type = "新增",module = "PestsProjectController",requestDes = "新增项目")
    @PostMapping("save")
    public Result save(@RequestBody PestsProject project) {
        boolean save = service.save(project);
        if(save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //根据讲师id进行查询
    @ApiOperation(value = "根据ID查询一条数据")
    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "根据ID查询一条数据")
    @GetMapping("getOne/{id}")
    public Result getOne(@PathVariable String id) {
        PestsProject eduTeacher = service.getById(id);
        return Result.ok().data("teacher",eduTeacher);
    }

    /**
     *
     * @param userId
     * @param projectId
     * @return
     */
    @ApiOperation(value = "用户切换项目")
    @OperLog(type = "功能",module = "PestsProjectController",requestDes = "用户切换项目")
    @GetMapping("switchProject/{userId}/{projectId}")
    public Result switchProject(@PathVariable String userId,@PathVariable String projectId) {
        //构建条件
        UcenterMember member = memberService.getById(userId);
        PestsProject pestsProject = service.getById(projectId);
        if (null != member && null != pestsProject){
            pestsProject.setUserId(member.getId());
            return Result.ok().data("row",pestsProject);
        }else{
            return Result.error();
        }
    }
    //根据讲师id进行查询
    @ApiOperation(value = "查询一条Delete为0的数据")
    @OperLog(type = "查询",module = "PestsProjectController",requestDes = "查询一条Delete为0的数据")
    @GetMapping("getOneDeleted/{id}")
    public Result getOneDeleted(@PathVariable String id) {
        //构建条件
        QueryWrapper<PestsProject> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        PestsProject eduTeacher = service.getOne(wrapper,false);
        return Result.ok().data("teacher",eduTeacher);
    }
    //讲师修改功能
    @ApiOperation(value = "更新一条记录")
    @OperLog(type = "更新",module = "PestsProjectController",requestDes = "更新一条记录")
    @PostMapping("update")
    public Result update(@RequestBody PestsProject project) {
        boolean flag = service.updateById(project);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //讲师修改功能
    @ApiOperation(value = "初始化一条项目记录、用户与项目关系的记录")
    @OperLog(type = "功能",module = "PestsProjectController",requestDes = "初始化一条项目记录、用户与项目关系的记录")
    @PostMapping("init")
    public Result init(String userId) {
        PestsProject flag = service.initProject(userId);
        return Result.ok().data("row",flag);
    }
}

